package com.quantstack.llcommonlibrary.dialog;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.quantstack.llcommonlibrary.R;
import com.quantstack.llcommonlibrary.manager.DialogManager;
import com.quantstack.llcommonlibrary.manager.DialogView;
import com.quantstack.llcommonlibrary.utils.LogUtils;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/3/2 22:04
 * @Notes 打开系统设置弹窗
 */
public class SettingDialog {
    private DialogView settingDialog;
    public static final int DEFAULT_SETTINGS_REQ_CODE = 1000;

    public SettingDialog(final Activity context) {
        //初始化弹框
        settingDialog = DialogManager.getInstance().initView(context, R.layout.dialog_setting);
        settingDialog.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getInstance().hide(settingDialog);
                getAppDetailSettingIntent(context);
                if(onClickListener!=null){
                    onClickListener.onOk();
                }
            }
        });
        settingDialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getInstance().hide(settingDialog);
                if(onClickListener!=null){
                    onClickListener.onCancel();
                }
            }
        });
    }

    public void showDialog(){
        if(settingDialog!=null){
            DialogManager.getInstance().show(settingDialog);
        }
    }

    public void hideDialog(){
        if(settingDialog!=null){
            DialogManager.getInstance().hide(settingDialog);
        }
    }

    public OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener{
        void onOk();
        void onCancel();
    }

    /**
     * 跳转到权限设置界面
     */
    private void getAppDetailSettingIntent(Activity activity){
        LogUtils.e("跳转");
        Intent intent = new Intent();
        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        activity.startActivityForResult(intent,DEFAULT_SETTINGS_REQ_CODE);
    }
}
